const util = require('../../../../../../utils/util.js');
const api = require('../../../../../../config/api.js');
const app = getApp();
Page({
  data: {
    zdyTagName: '',  //接收旧标签名
    olbTagindex: null,  //接收旧标签的Index
    zdyShow: false,   //控制自定义对话框
    logininfo: {},
    selSum: 0,
    //  标签集合    
    tagList: [],
    //用户选择的标签集合
    userTagList: [],
  },
    onLoad: function () {
      var _this = this;
      var openid = app.globalData.logininfo.openid;
      var data = {};
      util.post(api.GetAllBasWho).then(function (res) {
        if (res.code == 200) {
          for (var i = 0; i < res.result.list.length;i++){
            res.result.list[i].type=1
          }
          _this.setData({
            tagList: res.result.list
          })
        } 
      })
      if (openid != null && openid != "") {
        util.post(api.fetchToken()).then(function (res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          data.openid = openid;
          data.access_token = res.access_token;
          util.post(api.GetUserWhoInfo, data).then(function (res) {
          if (res.code == 200) {
            var tags = _this.data.tagList
            var sel = res.result.list
            for (var i = 0; i < sel.length; i++) {
              if (sel[i].type == 2) {
                tags.push({ "id": null, "type": 2, "catalogname": sel[i].catalogname, "checked": true })
              }
            }
            _this.setData({
              tagList: tags,
              userTagList: sel
            })
            _this.checkselect();
            _this.checkNumber();
          }
        });
      });
    }
  },
  //检查系统标签选中情况
  checkselect: function(){
    var _this = this
    var tags = _this.data.tagList
    for (var j = 0; j < tags.length; j++) {
      if (tags[j].type==1){
        tags[j].checked = false
      }
    }
    var sel = _this.data.userTagList
    for(var i = 0;i<sel.length;i++){
      for(var j=0;j<tags.length;j++){
        if (sel[i].whoid==tags[j].id){
          tags[j].checked = true
        }
      }
    }
    _this.setData({
      tagList: tags
    })
  },
  //系统自带标签的选择和取消选中
  selecttag: function (e) {
    var _this = this
    var sId = e.currentTarget.dataset.id
    var count = _this.data.selSum
    var tags = _this.data.tagList
    for (var i = 0; i < tags.length;i++){
      if (sId == tags[i].id){
        if (tags[i].checked){
          tags[i].checked = false
        }else{
          if (count<9){
            tags[i].checked = true
          }else{
            wx.showToast({
              title: '最多选择9个标签',
              icon: 'none',
              duration: 1500
            })
          }
        }
      }
    }
    _this.setData({
      tagList: tags
    })
    _this.checkNumber();
  },
  //开启标签输入
  zdyTagShow: function () {
    var _this = this
    var arr = _this.data.tagList
    var count = _this.data.selSum
    if (count < 9) {
      this.setData({
        zdyShow: true
      });
    } else {
      wx.showToast({
        title: '最多选择9个标签',
        icon: 'none',
        duration: 1500
      })
    }
  },
  //点击自定义标签进入编辑对话框
  bianjiTag: function (e) {
    var sIndex = e.currentTarget.dataset.index
    var sName = e.currentTarget.dataset.name
    var _this = this
    this.setData({
      zdyTagName: sName,
      olbTagindex: sIndex,
      zdyShow: true
    })
  },
  // 获取自定义标签对话框输入的值
  newTag: function (e) {
    var nTag = e.detail.value.replace(/\s+/g, '')
    this.setData({
      zdyTagName: nTag
    })
  },
  //新增或修改标签
  addTag: function (e) {
    var _this = this
    var sIndex = e.currentTarget.dataset.index
    var newTag = _this.data.zdyTagName
    var tags = _this.data.tagList
    var openadd = true
    for(var i =0;i<tags.length;i++){
      if (tags[i].catalogname == newTag){
        openadd = false
      }
    }
    if (openadd){
    if (sIndex != null && sIndex != ''){
      if (newTag != null && newTag != '') {
        tags[sIndex].catalogname = newTag
      } else {
        tags.splice(sIndex, 1)
      }
    }else{
      tags.push({ "id": null, "type": 2, "catalogname": newTag, "checked": true })
    }
    this.setData({
      tagList: tags,
      zdyTagName: null,
      olbTagindex: null,
      zdyShow: false
    })
    }else{
      wx.showToast({
        title: '已有此类标签',
        icon: 'none',
        duration: 1500
      })
    }
    _this.checkNumber();
  },
  //关闭标签输入
  closeTag: function () {
    this.setData({
      zdyTagName: null,
      olbTagindex: null,
      zdyShow: false
    });
    this.checkNumber();
  },
  //获取选择数量
  checkNumber: function(){
    var _this = this
    var count = 0
    var tags = _this.data.tagList
    for(var i =0;i<tags.length;i++){
      if (tags[i].checked){
        count++
      }
    }
    _this.setData({
      selSum: count
    });
  },
  ///下一步
  getTag: function () {
    var _this = this
    var openid = app.globalData.logininfo.openid;
    var data = {};
    var tags = _this.data.tagList
    var arr = []
    for(var i = 0;i<tags.length;i++){
      if (tags[i].checked){
        var id = tags[i].id
        var type = tags[i].type
        var catalogname = tags[i].catalogname
        arr.push({ "whoid": id, "type": type, "catalogname": catalogname})
      }
    }
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        data.basWhoList = JSON.stringify(arr)
        util.post(api.UpdateWhoSelected, data).then(function (res) {
          if (res.code == 200) {
            wx.navigateTo({
              url: '../interesttag2/interesttag2',
            })
          }
        });
      });
    }
  }
})
